package demo;

public class Sort {
    //插入排序
    public static void inserSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0 ; j--) {
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    //冒泡排序
    public static void bublleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flg = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flg = false;
                }
            }
            if (flg){
                break;
            }
        }
    }
    public static void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    //选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }
    //希尔排序:分组+插入
    public static void shellSort(int[] array){
        int gap = array.length;
        while (gap > 1){
            gap /= 2;
            shell(array, gap);
        }
    }
    public static void shell(int[] array, int gap){
        for (int i = gap; i <array.length ; i = i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0 ; j-=gap) {
                if (array[j] > tmp){
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    //堆排序：升序需要创建大根堆
    public static void heapSort(int[] array){
        createBigHeap(array);
        //将堆顶元素与最后一个元素进行交换
        int end = array.length;
        while (end > 0){
            swap(array, 0, end - 1);
            shiftDown(array, 0, end - 1);
            end--;
        }
    }
    //创建大根堆
    public static void createBigHeap(int[] array){
        for (int i = (array.length - 1 - 1) / 2; i >= 0 ; i--) {
            shiftDown(array, i, array.length);
        }
    }
    //向下调整实现大根堆
    public static void shiftDown(int[] array, int parent, int len){
        int child = 2*parent + 1;
        while (child < len){
            if (child + 1 < len && array[child] < array[child + 1]){
                child = child + 1;
            }
            if (array[child] > array[parent]){
                swap(array, parent, child);
            }else {
                break;
            }
            parent = child;
            child = 2*parent + 1;
        }
    }

    //快速排序
    public static void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }
    public static void quick(int[] array, int left, int right){
        if (left >= right){
            return;
        }
        int pivot = parttion(array, left, right);
        quick(array, left, pivot - 1);
        quick(array, pivot + 1, right);
    }
    public static int parttion(int[] array, int left, int right){
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }
    //快速排序优化
    //1.求中数
    public static void quickSort1(int[] array){
        quick(array, 0, array.length - 1);
    }
    public static void quick1(int[] array, int left, int right){
        if (left >= right){
            return;
        }
        int mid = threeNum(array, left, right);
        swap(array, mid, left);
        int pivot = parttion1(array, left, right);
        quick(array, left, pivot - 1);
        quick(array, pivot + 1, right);
    }
    public static int threeNum(int[] array, int left, int right){
        int mid = (left + right) /2;
        if (array[left] < array[right]){
            if (array[mid] < array[left]){
                return left;
            }else if (array[mid] > array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] > array[left]){
                return left;
            }else if (array[mid] < array[right]){
                return right;
            }else {
                return mid;
            }
        }
    }
    public static int parttion1(int[] array, int left, int right){
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }
    //2.递归到小的子区间用插入排序
    public static void quickSort2(int[] array){
        quick2(array, 0, array.length - 1);
    }
    public static void quick2(int[] array, int left, int right){
        if (left >= right){
            return;
        }
        if ((right - left + 1) <= 20){
            inserSort(array);
            return;
        }
        int pivot = parttion2(array, left, right);
        quick2(array, left, pivot - 1);
        quick2(array, pivot + 1, right);
    }
    public static int parttion2(int[] array, int left, int right){
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }
    //两种优化整合
    public static void quick3(int[] array, int left, int right){
        if (left >= right){
            return;
        }
        if ((right - left + 1) <= 20){
            inserSort(array);
            return;
        }
        int mid = threeNum(array, left, right);
        swap(array, mid, left);
        int pivot = parttion2(array, left, right);
        quick3(array, left, pivot - 1);
        quick3(array, pivot + 1, right);
    }
    //归并排序
    public static void mergeSort(int[] array){
        mergeSortFunc(array, 0, array.length - 1);
    }
    public static void mergeSortFunc(int[] array, int left, int right){
        if (left >= right){
            return;
        }
        int mid = (left + right) / 2;
        mergeSortFunc(array, left, mid);
        mergeSortFunc(array, mid + 1, right);
        merge(array,left, mid, right);
    }
    public static void merge(int[] array, int left, int mid, int right){
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int[] tmpArray = new int[right - left + 1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2){
           if (array[s1] <= array[s2]){
               tmpArray[k++] = array[s1++];
           }
           if (array[s2] <= array[s1]){
               tmpArray[k++] = array[s2++];
           }
        }
        while (s1 <= e1){
            tmpArray[k++] = array[s1++];
        }
        while (s2 <= e2){
            tmpArray[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i + left] = tmpArray[i];
        }
    }
    //技术排序
    public static void countSort(int[] array) {
        int min_value = array[0];
        int max_value = array[0];
        //获取序列所在的数值范围
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min_value) {
                min_value = array[i];
            }
            if (array[i] > max_value) {
                max_value = array[i];
            }
        }
        int range = max_value - min_value + 1;
        //创建计数数组
        int[] countArray = new int[range];
        for (int i = 0; i < array.length; i++) {
            countArray[array[i] - min_value]++;
        }
        //计数数组和原数组的对应关系存好了，遍历计数数组实现原数组的排序
        int index = 0;//array数组的下标
        for (int i = 0; i < range; i++) {
            int val = countArray[i];
            while (val != 0) {
                array[index] = i + min_value;
                index++;
                val--;
            }
        }
    }
}
